package com.whu.exp1jsp.javabean;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

public class TimeCorrect {

    public String correctTime(LocalDateTime wrongTime) {
        try {
            // 转换为指定时区的 ZonedDateTime 对象
            ZonedDateTime utcDateTime = wrongTime.atZone(ZoneId.of("UTC"));

            // 转换为上海时区的 ZonedDateTime 对象
            ZonedDateTime shanghaiDateTime = utcDateTime.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));

            // 设置输出格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

            // 格式化输出
            String formattedDateTime = shanghaiDateTime.format(formatter);

            return formattedDateTime;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
